﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;
using System.Data;
using RenShiPinYongBiao.hrdbDataSetTableAdapters;
using System.Windows.Forms;
using System.IO;

namespace RenShiPinYongBiao
{
    class WordExporter
    {
        private DataTable dt;
        
        private int index = 0;
        private List<string> texts;
        public WordExporter(DataTable dt)
        {
            this.index = 0;
            this.dt = dt;
            
        }
        public void Export(string fileName)
        {
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("没有内容，不需要导出");
                return;
            }

            WordHelp wh = new WordHelp();
            wh.Open();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                _Export(fileName, wh);
                if (index < dt.Rows.Count - 1)
                {
                    wh.InsertPagebreak();
                }
                index++;
            }
            wh.SaveAs(fileName);
            wh.Quit();
        }
        private void _Export(string fileName,WordHelp wh)
        {
            Microsoft.Office.Interop.Word.Application app = wh.WordApplication;
            Document doc = wh.WordDoc;
            doc.PageSetup.TopMargin = 60f;
            doc.PageSetup.BottomMargin = 60f;
            doc.PageSetup.LeftMargin = 45f;
            doc.PageSetup.RightMargin = 45f;
            wh.InsertText("事业单位聘用人员登记表");
            app.Selection.HomeKey(WdUnits.wdLine, WdMovementType.wdExtend);
            app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            app.Selection.Range.Font.Size = 16;
            app.Selection.EndKey(WdUnits.wdLine, WdMovementType.wdMove);
            wh.InsertLineBreak();

            //doc.Paragraphs[1 + (index) * 47].Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            //doc.Paragraphs[1 + (index) * 47].Range.Font.Size = 16;
            app.Selection.Range.Font.Size = 12;
            wh.InsertText("    序号：");
            wh.InsertText(dt.Rows[index]["FEmployeeID"].ToString());
            wh.InsertText("                                           增人计划卡：");
            app.Selection.HomeKey(WdUnits.wdLine, WdMovementType.wdExtend);
            app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            app.Selection.Range.Font.Size = 12;
            app.Selection.EndKey(WdUnits.wdLine, WdMovementType.wdMove);
            wh.InsertLineBreak();
            wh.InsertLineBreak();
            //doc.Paragraphs[2 +(index) * 47].Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            //doc.Paragraphs[2 +(index) * 47].Range.Font.Size = 12;
            //wh.InsertLineBreak();
            Table t = null;
            if (index == 0)
            {
                t = doc.Tables.Add(app.Selection.Range, 7, 9);
                t.Borders.Enable = 1;
                t.Columns.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthPercent;
                t.Rows.HeightRule = WdRowHeightRule.wdRowHeightAtLeast;
                t.Rows.Height = 40;
                t.Range.Font.Size = 12;
                t.Rows.Alignment = WdRowAlignment.wdAlignRowCenter;
                t.Select();
                app.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                for (int i = 1; i <= 8; i++)
                {
                    t.Columns[i].PreferredWidth = 10.8108f;
                }
                t.Columns[9].PreferredWidth = 13.5135f;
                t.Cell(1, 9).Merge(t.Cell(2, 9));
                //毕业院校
                t.Cell(3, 1).Merge(t.Cell(3, 2));
                for (int i = 1; i <= 3; i++)
                {
                    t.Cell(3, 2).Merge(t.Cell(3, 3));
                }
                t.Cell(3, 4).Merge(t.Cell(3, 5));
                //聘用前工作单位
                t.Cell(4, 1).Merge(t.Cell(4, 2));
                for (int i = 1; i <= 6; i++)
                {
                    t.Cell(4, 2).Merge(t.Cell(4, 3));
                }
                //拟聘岗位及等级
                t.Cell(5, 1).Merge(t.Cell(5, 2));
                for (int i = 1; i <= 3; i++)
                {
                    t.Cell(5, 2).Merge(t.Cell(5, 3));
                }
                t.Cell(5, 4).Merge(t.Cell(5, 5));

                //工作简历
                //for (int j = 6; j <= 8; j++)
                //{
                //    for (int i = 1; i <= 7; i++)
                //    {
                //        t.Cell(j, 2).Merge(t.Cell(j, 3));
                //    }
                //}
                //t.Cell(6, 2).Merge(t.Cell(7, 2));
                //t.Cell(6, 2).Merge(t.Cell(8, 2));
                //t.Cell(6, 1).Merge(t.Cell(7, 1));
                //t.Cell(6, 1).Merge(t.Cell(7, 1));
                for (int i = 1; i <= 7; i++)
                {
                    t.Cell(6, 2).Merge(t.Cell(6, 3));
                }
                t.Cell(6, 1).Select();
                app.Selection.SelectRow();
                app.Selection.Rows.Height = 160;
                t.Cell(7, 1).Select();
                app.Selection.SelectRow();
                app.Selection.Rows.Height = 240;

                //家庭成员及社会关系
                for (int i = 1; i <= 7; i++)
                {
                    t.Cell(7, 2).Merge(t.Cell(7, 3));
                }
            }
            else
            {
                app.Selection.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                t = doc.Tables[index + 1];
            }
            

            //文本赋值
            DataSourcePinYong source = new DataSourcePinYong(this.dt);
            texts=source.PrepareText(index);
            int textsindex = 0;
            for (int j = 1; j <= 2; j++)
            {
                for (int i = 1; i <= 8; i++)
                {
                    t.Cell(j, i).Range.Text = texts[textsindex++];
                }
            }
            for (int i = 1; i <= 4; i++)
            {
                t.Cell(3, i).Range.Text = texts[textsindex++];
            }
            t.Cell(4, 1).Range.Text = texts[textsindex++];
            t.Cell(5, 1).Range.Text = texts[textsindex++];
            for (int i = 1; i <= 4; i++)
            {
                t.Cell(5, i).Range.Text = texts[textsindex++];
            }
            t.Cell(6, 1).Select();
            app.Selection.Orientation = WdTextOrientation.wdTextOrientationVertical;
            t.Cell(6, 2).Select();
            app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            t.Cell(6, 2).LeftPadding = 30;
            t.Cell(7, 2).Select();
            app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            t.Cell(7, 2).LeftPadding = 30;
            t.Cell(6, 1).Range.Text = texts[textsindex++];
            t.Cell(6, 2).Range.Text = texts[textsindex++];
            t.Cell(7, 1).Range.Text = texts[textsindex++];
            t.Cell(7, 2).Range.Text = texts[textsindex++];
            //照片粘贴
            t.Cell(1, 9).Select();
            app.Selection.Paste();
            t.Select();
            app.Selection.Copy();
            app.Selection.MoveDown(WdUnits.wdLine, 1);
            texts.Clear();
        }
       
        
    }
}
